#include <cstdio>
#include<unistd.h>
#include <sys/wait.h>
#include <cstdlib>
#include <iostream>
using namespace std;

// int main(){
//     //函数没有p的就需要我们写全路径
//     cout << "我是原进程, pid: " << getpid() << endl;
//     // cout << "execl: 执行ls" << endl;
//     // execl("/usr/bin/ls","ls","-al","--color=auto",nullptr);
//     cout << "execl: 执行other" << endl;
//     execl("./other","other",nullptr);
//     return 0;
// }

//多进程替换
int main(){
    pid_t id = fork();
    if (id > 0){
        while(1){
            int status = 0;
            pid_t child = waitpid(id,&status,WNOHANG);
            if (child > 0){
                printf("wait %d success, status:%d, errorcode:%d\n",child,(status>>8)&0xFF,status&0x7F);
                break;
            }
            else if(child == 0){
                printf("%s\n","子进程还没推出");
                continue;
            }
            else {
                printf("等待失败\n");
            }
            sleep(1);
        }
    }
    else {
        cout << "我是子进程,pid: " << getpid() << endl;
        execl("/usr/bin/ls","ls","-al","--color=auto",nullptr);
    }
    return 0;
}
